package com.sls.api.game.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.sls.api.game.constant.Constant;
import com.sls.api.game.pojo.XinHeInfo;
import com.sls.api.util.IpUtil;
import com.sls.api.util.PropertiesUtil;
import com.sls.api.util.UtilMD5Encryption;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;

public class XinHeTemplateUpdateServlet extends SuperServlet {

	private static final long serialVersionUID = 4228975706724846750L;
	private static Logger log = Logger
			.getLogger(XinHeTemplateUpdateServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setAttribute("org.eclipse.jetty.server.Request.queryEncoding",
				"UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		resp.setStatus(HttpServletResponse.SC_OK);

		String companyId = req.getParameter("companyId");
		String userId = req.getParameter("userId");
		String version = req.getParameter("version");
		String msg = req.getParameter("msg");
		String sign = req.getParameter("sign");
		log.info("鑫合充值模板更新协议通知的参数：" + "companyId：" + companyId + ","
				+ "userId：" + userId + "," + "version：" + version + ","
				+ "msg：" + msg + "," + "sign：" + sign);
		boolean flag = this.checkData(companyId, version, msg, sign,userId, resp);
		if (flag) {
			try {
				SocketDTO dto = this.sendMsg(companyId, version, msg,userId, req);
				Head head = dto.getHead();
				String resCode = head.getResponseCode();
				if (Constant.SUCCESS.equals(resCode)) {
					resp.getWriter().write("success");
					close(resp.getWriter());
				} else {
					resp.getWriter().write("未知错误!");
					close(resp.getWriter());
				}
			} catch (Exception e) {
				resp.getWriter().write("未知错误!");
				close(resp.getWriter());
			}
		}

	}

	private SocketDTO sendMsg(String companyId, String version, String msg,
			String userId, HttpServletRequest req) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021016");
		head.setIp(IpUtil.getIpAddr(req));
		head.setUserId("0");
		XinHeInfo info = new XinHeInfo();
		info.setCompanyId(companyId);
		info.setUserId(userId);
		info.setVersion(version);
		info.setMsg(msg);
		return super.send(head, info);
	}

	private boolean checkData(String companyId, String version, String msg,
			String sign, String userId, HttpServletResponse resp) throws IOException {
		boolean result = false;
		if (StringUtils.isEmpty(companyId)) {
			resp.getWriter().write("缺少必要参数[companyId]!");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(userId)) {
			resp.getWriter().write("缺少必要参数[userId]!");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(version)) {
			resp.getWriter().write("缺少必要参数[version]!");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(sign)) {
			resp.getWriter().write("缺少必要参数[sign]!");
			close(resp.getWriter());
			return false;
		} else {
			String data = companyId;
			if (StringUtils.isNotBlank(msg)) {
				data += msg;
			}
			data += userId;
			data += version;
			String md5key = PropertiesUtil.getValue("xinhe.md5.key");
			String mySign = UtilMD5Encryption.createMD5EncryptionOfUTF8(data
					+ md5key);
			if (sign.equals(mySign)) {
				result = true;
			} else {
				resp.getWriter().write("签名验证失败!");
				close(resp.getWriter());
				return false;
			}
		}
		return result;
	}

	/**
	 * 关闭资源
	 * 
	 * @param pw
	 */
	private void close(PrintWriter pw) {
		if (pw != null) {
			pw.flush();
			pw.close();
		}
	}
}
